﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Data.SqlClient;
using System.Data.Common;

namespace CH.Study.ConsoleApp.ExpressionTree
{
    #region QueryExpressionMethodExtend
    public static class ExpressionMethodExtend
    {
        public static SqlParameter[] ToSqlParameterArray(this DbParameter[] dbParams)
        {
            int length = dbParams.Count();
            SqlParameter[] sqlParams = new SqlParameter[length];
            for (int i = 0; i < length; i++)
            {
                sqlParams[i] = (SqlParameter)dbParams[i];
            }
            return sqlParams;
        }

        public static QueryExpressionDebug Debug<TSource>(this IQueryable<TSource> source)
        {
            IQueryExpressionProvider iep = source.Provider as IQueryExpressionProvider;
            return iep.GetQueryExpressionDebugMsg(source.Expression);
        }

        public static QueryPageList<TSource> ToQueryPageList<TSource>(this IQueryable<TSource> source)
        {
            QueryQueryable<TSource> qq = source as QueryQueryable<TSource>;
            return new QueryPageList<TSource>(qq);
        }




        /// <summary>
        /// Order
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static OrderType Order(Expression expression, OrderType type)
        {
            return type;
        }


        /// <summary>
        /// Like
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool Like(this string obj, string value)
        {
            return true;
        }

        /// <summary>
        /// In
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool In(this int? obj, int[] array)
        {
            return true;
        }


        /// <summary>
        /// NotIn
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool NotIn(this int? obj, int[] array)
        {
            return true;
        }

        /// <summary>
        /// As
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static T As<T>(this T obj, string value)
        {
            return obj;
        }

        /// <summary>
        /// Count
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int? Count(this int? obj)
        {
            return obj;
        }


        /// <summary>
        /// Sum
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int? Sum(this int? obj)
        {
            return obj;
        }

        /// <summary>
        /// Max
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static T Max<T>(this T obj)
        {
            return obj;
        }

        /// <summary>
        /// Min
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static T Min<T>(this T obj)
        {
            return obj;
        }



        /// <summary>
        /// 移除最后一个字符串
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string RemoveLastString(this string str, string removeString)
        {

            if (string.IsNullOrEmpty(str))
            {
                return string.Empty;
            }


            int index = str.LastIndexOf(removeString);
            if (index != -1)
            {
                return str.Substring(0, index);
            }
            else
            {
                return str;
            }

        }
    }
    #endregion
}
